var timer = null,
	speedY = 0,
	bird = document.querySelector(".bird"),
	bg_white = document.querySelector(".bg_white"),
	bigtimer,
	smalltimer,
	saw = 0,
	p = document.querySelector("p"),
	body = document.querySelector(".web");
	
	
	
	
	
	
	
	//随机数
	function rand(min,max){
		return parseInt(Math.random()*(max-min+1)+min);
	}
	
	
	function pipe(){
		//创建节点
		//上管
		var pipe_top = document.createElement("div");
		pipe_top.className = "pipe_top";
		//自定义属性
		pipe_top.num = "100";//移动每次-1,原来设置的总数
		pipe_top.setAttribute("deta_dd","true");
		
		var num_height = rand(50,300);//随机高度
		pipe_top.style.height = num_height +"px";
		
		bg_white.appendChild(pipe_top);//添加
		
		//下管
		var pipe_bottom = document.createElement("div");
		pipe_bottom.className = "pipe_bottom";
		
		pipe_bottom.style.height = bg_white.offsetHeight - num_height - 150 +"px";
		pipe_bottom.style.top = num_height + 150 + "px";
		
		bg_white.appendChild(pipe_bottom);
	
		
	}
	
	
	
	smalltimer = setInterval(pipe,1500);
	
	clearInterval(bigtimer);
	
	bigtimer = setInterval(function(){
		var alltop = document.querySelectorAll(".pipe_top"),
			allbottom = document.querySelectorAll(".pipe_bottom");
			
			
		for (var i = 0; i < alltop.length; i++) {
			var bird_1  = bird.offsetLeft,
				allpipe = alltop[i].offsetLeft+alltop[i].offsetWidth;
			
			
//			碰撞检测
			if(crash(bird,alltop[i])||crash(bird,allbottom[i])){
				die = true;
				
				clearInterval(bigtimer);
				clearInterval(smalltimer);
			}
			
			//
			if(alltop[i].num == "-10"){
				bg_white.removeChild(alltop[i]);
				bg_white.removeChild(allbottom[i]);
				continue;
			}
			
			if(bird_1>allpipe){
				
				if(alltop[i].getAttribute("deta_dd")){
					saw++;
					alltop[i].removeAttribute('deta_dd');
				}
				p.innerText = saw;
				console.log(saw);
				
			}
			
			
		allbottom[i].style.left=alltop[i].style.left = alltop[i].num-1 + "%";
		alltop[i].num = alltop[i].num-1;
		}
},30);

	



	var die = false,
		timer;


//自由落体
	
	timer = setInterval(function(){
		speedY++;		
		
		var t = bird.offsetTop + speedY;
		bird.style.top = t + "px";
		
		if(t<0){
			die = true;
			t = 0;
			speedY = 0;
			clearInterval(bigtimer);
			clearInterval(smalltimer);
		}
		if(t>bg_white.offsetHeight){
			die = true;
			speedY = 0;
			t = 10;
			clearInterval(bigtimer);
			clearInterval(smalltimer);
			clearInterval(timer);
			alert("你的总得分"+saw);
		}
		
	},50)
	
	this.addEventListener("touchstart",function(){
		if(die){
			return;
		}
		speedY -=15;
		if(speedY<-10){
			speedY=-10
		}
	});